//
//  PCLocationView.m
//  EP
//
//  Created by yanfa on 15/8/13.
//  Copyright (c) 2015年 Sun. All rights reserved.
//

#import "PCLocationView.h"

#define kDuration 0.3

@implementation PCLocationView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(id)initWithTitle:(NSString *)title delegate:(id)delegate{
    self = [[[NSBundle mainBundle] loadNibNamed:@"PCLocationView" owner:self options:nil] objectAtIndex:0];
    self.delegate = delegate;
    _labelTitle.text = title;
    _locateView.delegate = self;
    _locateView.dataSource = self;
    lots = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"lot.plist" ofType:nil]];
    _type = 0;
    return self;
}

-(void)showInView:(UIView *)view withSelectIndex:(NSInteger)selectIndex{
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromTop;
    [self setAlpha:1.0f];
    [self.layer addAnimation:animation forKey:nil];
    self.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    [_locateView selectRow:selectIndex inComponent:0 animated:YES];
    [view addSubview:self];
}

-(void)showInView:(UIView *)view withType:(NSInteger)type withSelectIndex:(NSInteger)sid{
    _type = type;
    _selectIndex = sid;
    if(_type == 1){
        NSDate *now = [NSDate date];
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
        NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
        int year = [dateComponent year];
        lots = @[[NSString stringWithFormat:@"%d",year-2],[NSString stringWithFormat:@"%d",year-1],[NSString stringWithFormat:@"%d",year]];
        _selectStr = [lots objectAtIndex:sid];
    }else if(_type == 2){
        lots = @[@"全部",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
        _selectStr = [lots objectAtIndex:sid];
    }
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromTop;
    [self setAlpha:1.0f];
    [self.layer addAnimation:animation forKey:nil];
    self.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    [_locateView selectRow:sid inComponent:0 animated:YES];
    [view addSubview:self];
}

#pragma mark  dsfsdfss

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [lots count];
//    switch (component) {
//        case 0:
//            NSLog(@"%d",[lots count]);
//            return [lots count];
//            break;
//        default:
//            return 0;
//            break;
//    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            if(_type == 0){
                return [[lots objectAtIndex:row] objectForKey:@"name"];
            }else{
                return [lots objectAtIndex:row];
            }
            
            break;
        default:
            return nil;
            break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (component) {
        case 0:
            if(_type == 0){
                _selectIndex = row;
                _selectLotId = [[lots objectAtIndex:row] objectForKey:@"id"];
                _selectLotName = [[lots objectAtIndex:row] objectForKey:@"name"];
            }else{
                _selectIndex = row;
                _selectStr = [lots objectAtIndex:row];
            }
            
            break;
        default:
            break;
    }
}



-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self btnClick_cancel:nil];
}


- (IBAction)btnClick_cancel:(id)sender {
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromBottom;
    [self setAlpha:0.0f];
    [self.layer addAnimation:animation forKey:nil];
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:kDuration];
    if(self.delegate) {
        [self.delegate actionSheet:self clickedButtonAtIndex:0];
    }
}

- (IBAction)btnClick_ok:(id)sender {
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromBottom;
    [self setAlpha:0.0f];
    [self.layer addAnimation:animation forKey:@"TSLocateView"];
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:kDuration];
    if(self.delegate) {
        [self.delegate actionSheet:self clickedButtonAtIndex:1];
    }
}
@end
